﻿using System;
using System.Collections.Generic;
using System.Text;
using Xeno.Core;
using Xeno.Maths;
using Xeno.Module;
using Xeno.GUI;

using OpenTK;
using OpenTK.Audio;

using OpenTK.Input;
using OpenTK.Platform;
using OpenTK;
using Komires.MataliPhysics;
namespace Xeno.Physics
{
    public class XenoRigidBody : XenoModule
    {
        public IntPtr Actor { get; set; }
        public PhysicsObject PO { get; set; }

        public XenoRigidBody(string name,XenoNode attached,bool isStatic,float mass)
        {
          
            Attached = attached;
            XenoCollider cod = Attached.Collider;
     
            List<XenoNode> managers = Attached.GraphOwner.RootNode.AllWithType(typeof(XenoPhysicsManager), null);
            XenoNode man = managers[0];

            Matrix4 mat = attached.LocalRotation;

        //    Actor = XP.ActorWithCollider(cod.Shape, isStatic, attached.LocalPosition.X, attached.LocalPosition.Y, attached.LocalPosition.Z, mat.M11, mat.M12, mat.M13, mat.M21, mat.M22, mat.M23, mat.M31, mat.M32, mat.M33);
            PO = XenoPhysicsManager.Instance.PS.Factory.PhysicsObjectManager.Create(attached.Name + "_PhysicsObject001");
            PO.Shape = cod.S;
            PO.UserTagStr = attached.Name + "_PhysicsObject001";
            PO.InitLocalTransform.SetPosition(attached.WorldPosition);
            PO.InitLocalTransform.SetScale(attached.LocalScale);
            PO.InitLocalTransform.SetRotation(attached.WorldRotationMatrix);
            PO.Integral.SetDensity(isStatic ? 0.0f : mass);
            if (!isStatic)
            {
                PO.Integral.SetMass(mass);
             
            }

            XenoPhysicsManager.Instance.PS.UpdateFromInitLocalTransform(PO);


            XenoCollider.ShapeMap[XP.GetActor(cod.Shape)] = attached;
           


        }

        public override void DoUpdate()
        {

            Vector3 pos = Vector3.Zero;

            PO.InitLocalTransform.GetPosition(ref pos);

            Attached.LocalPosition = pos;


            Matrix4 rot = Matrix4.Identity;

            PO.InitLocalTransform.GetRotation(ref rot);

            Attached.LocalRotation = rot;




        }

    }
}
